package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Print("> ")
		input, _ := reader.ReadString('\n')
		input = strings.TrimSpace(input)
		args := strings.Fields(input)

		if len(args) == 0 {
			continue
		}

		switch args[0] {
		case "ls":
			listDir(".")
		case "mkdir":
			if len(args) < 2 {
				fmt.Println("Usage: mkdir <dirname>")
				continue
			}
			createDir(args[1])
		case "touch":
			if len(args) < 2 {
				fmt.Println("Usage: touch <filename>")
				continue
			}
			createFile(args[1])
		case "rm":
			if len(args) < 2 {
				fmt.Println("Usage: rm <filename>")
				continue
			}
			deleteFile(args[1])
		case "mv":
			if len(args) < 3 {
				fmt.Println("Usage: mv <oldname> <newname>")
				continue
			}
			renameFile(args[1], args[2])
		case "exit":
			fmt.Println("Exiting...")
			return
		default:
			fmt.Println("Unknown command")
		}
	}
}

// 列出当前目录内容
func listDir(dir string) {
	files, err := os.ReadDir(dir)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	for _, file := range files {
		fmt.Println(file.Name())
	}
}

// 创建目录
func createDir(dirname string) {
	err := os.Mkdir(dirname, 0755)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Directory created:", dirname)
}

// 创建文件
func createFile(filename string) {
	file, err := os.Create(filename)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	file.Close()
	fmt.Println("File created:", filename)
}

// 删除文件
func deleteFile(filename string) {
	err := os.Remove(filename)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("File deleted:", filename)
}

// 重命名文件
func renameFile(oldname, newname string) {
	err := os.Rename(oldname, newname)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Printf("File renamed: %s -> %s\n", oldname, newname)
}
